package com.ruoyi.project.system.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TDrugs;
import com.ruoyi.project.system.domain.TOrder;
import com.ruoyi.project.system.service.ITOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-05-11
 */
@RestController
@RequestMapping("/order")
public class TOrderController extends BaseController {
    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    @Autowired
    private ITOrderService tOrderService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('order:list')")
    @GetMapping("/list")
    public TableDataInfo list(TOrder tOrder) {
        sys_user_logger.info("查询订单请求参数",tOrder.toString());
        System.out.print("查询订单请求参数"+tOrder.toString());
        if (userRole(SecurityUtils.getLoginUser().getUser().getUserId())) {
            tOrder.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        }
        startPage();
        List<TOrder> list = tOrderService.selectTOrderNoList(tOrder);
        return getDataTable(list);
    }

    @PostMapping("/queryOrderDrugsByNo")
    public TableDataInfo queryOrderDrugsByNo(@RequestBody String orderNo) {
        startPage();
        List<TDrugs> list = tOrderService.getDrugsOrderList(orderNo);
        return getDataTable(list);
    }

    @GetMapping("/getOrderDetailed")
    public TableDataInfo getOrderDetailed(TOrder tOrder) {
        if (userRole(SecurityUtils.getLoginUser().getUser().getUserId())) {
            tOrder.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        }
        startPage();
        List<TOrder> list = tOrderService.getOrderDetailed(tOrder);
        return getDataTable(list);
    }

    /*  *//**
     * 导出【请填写功能名称】列表
     *//*
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TOrder tOrder)
    {
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        ExcelUtil<TOrder> util = new ExcelUtil<TOrder>(TOrder.class);
        return util.exportExcel(list, "order");
    }*/

    /* *//**
     * 获取【请填写功能名称】详细信息
     *//*
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tOrderService.selectTOrderById(id));
    }*/

    /* *//**
     * 新增【请填写功能名称】
     *//*
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOrder tOrder)
    {
        return toAjax(tOrderService.insertTOrder(tOrder));
    }*/

    /* *//**
     * 修改【请填写功能名称】
     *//*
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrder tOrder)
    {
        return toAjax(tOrderService.updateTOrder(tOrder));
    }*/

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('order:list:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tOrderService.deleteTOrderByIds(ids));
    }

    /**
     * 订单发货
     *
     * @param orderNo
     * @return
     */
    @PostMapping("/updateOrderShip")
    public AjaxResult updateOrderShip(@RequestBody String[] orderNo) {
        return toAjax(tOrderService.updateOrderShip(orderNo));
    }

    /**
     * 订单收货
     *
     * @param orderNo
     * @return
     */
    @PostMapping("/updateOrderReceipt")
    public AjaxResult updateOrderReceipt(@RequestBody String[] orderNo) {
        return toAjax(tOrderService.updateOrderReceipt(orderNo));
    }


}
